/**
 * 
 */
package pl.com.ozzy.ozsign.lang;

import java.util.Comparator;

import pl.com.ozzy.ozsign.tools.AppConfigManager;
import pl.com.ozzy.ozsign.ui.R;
import pl.com.ozzy.ozsign.ui.SignatureListAdapter;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.gesture.Gesture;
import android.gesture.GestureLibrary;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;

/**
 *
 */
public class SignaturesLoadTask extends AsyncTask<Void, NamedSignature, Integer> {
	
	private Context mContext;
	private SignatureListAdapter mAdapter;
	private ListActivity mActivity;
    private int mThumbnailSize;
    private int mThumbnailInset;
    private int mPathColor;

    private final Comparator<NamedSignature> mSorter = new Comparator<NamedSignature>() {
        public int compare(NamedSignature firstSignature, NamedSignature secondSignature) {
            return firstSignature.getName().compareTo(secondSignature.getName());
        }
    };
    
    public SignaturesLoadTask(Context context, SignatureListAdapter adapter, ListActivity activity) {
    	mContext = context;
    	mAdapter = adapter;
    	mActivity = activity;
    }
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        final Resources resources = mContext.getResources();
        mPathColor = resources.getColor(R.color.gesture_color);
        mThumbnailInset = (int) resources.getDimension(R.dimen.gesture_thumbnail_inset);
        mThumbnailSize = (int) resources.getDimension(R.dimen.gesture_thumbnail_size);

        mAdapter.setNotifyOnChange(false);            
        mAdapter.clear();
    }

    @Override
    protected Integer doInBackground(Void... params) {
        if (isCancelled()) return AppConfigManager.STATUS_CANCELLED;
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return AppConfigManager.STATUS_NO_STORAGE;
        }

        final GestureLibrary store = AppConfigManager.gStore;

        if (store.load()) {
            for (String name : store.getGestureEntries()) {
                if (isCancelled()) break;

                for (Gesture gesture : store.getGestures(name)) {
                    final Bitmap bitmap = gesture.toBitmap(mThumbnailSize, mThumbnailSize,
                            mThumbnailInset, mPathColor);
                    final NamedSignature NamedSignature = new NamedSignature();
                    NamedSignature.setGesture(gesture);
                    NamedSignature.setName(name);

                    mAdapter.addBitmap(NamedSignature.getGesture().getID(), bitmap);
                    publishProgress(NamedSignature);
                }
            }

            return AppConfigManager.STATUS_SUCCESS;
        }

        return AppConfigManager.STATUS_NOT_LOADED;
    }

    @Override
    protected void onProgressUpdate(NamedSignature... values) {
        super.onProgressUpdate(values);

        final SignatureListAdapter adapter = mAdapter;
        adapter.setNotifyOnChange(false);

        for (NamedSignature gesture : values) {
            adapter.add(gesture);
        }

        adapter.sort(mSorter);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        if (result == AppConfigManager.STATUS_NO_STORAGE) {
            mActivity.getListView().setVisibility(View.GONE);
            ((IUIListCallback)mActivity).executeOnPost();
        } else {
            checkForEmpty();
        }
    }
    
    private void checkForEmpty() {
        if (mAdapter.getCount() == 0) {
            ((IUIListCallback)mActivity).executeOnEmpty();
        }
    }
}